package titan.wizards.importTitanProject;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import titan.messages.Messages;
import titan.utils.SWTUtils;
import titan.utils.TitanUtils;
import titan.wizards.BaseTitanPage;

/**
 * Wizard page to import to Eclipse a Titan project
 */
public class ImportProjectPage extends BaseTitanPage {
	private Text projectLocation;
	private Button projectLocationButton;
	private boolean isDialogOpen;
	private TitanUtils titanUtils;
	//Status
	private IStatus projectLocationStatus;

	protected ImportProjectPage() {
		super(Messages.ImportTitanProject);
		setTitle(Messages.ImportTitanProject);
		setDescription(Messages.ImportProjectPage_desc);
		pageStatus = new Status(IStatus.OK, "default", ""); //$NON-NLS-1$ //$NON-NLS-2$
		isDialogOpen = false;
	}

	/**
	 * {@inheritDoc}
	 */
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(4, false);
		layout.verticalSpacing = 6;
		composite.setLayout(layout);
		setControl(composite);
		GridData gd = new GridData(280, 13);

		MouseTrackAdapter mouseExit = new MouseTrackAdapter() {
			public void mouseExit(MouseEvent e) {
			    applyStatus(pageStatus);
			}
		};

		new Label(composite, SWT.NONE).setText(Messages.ProjectLocation+":"); //$NON-NLS-1$
		projectLocation = new Text(composite, SWT.BORDER | SWT.SINGLE);
		projectLocation.setLayoutData(gd);
		projectLocation.addListener(SWT.MouseDown, new Listener () {
			public void handleEvent(Event e) {
				projectLocation.setCapture(false);
			}
		});
		projectLocation.addListener(SWT.FocusIn, new Listener () {
			public void handleEvent(Event e) {
					if(isDialogOpen){
						isDialogOpen = false;
						projectLocationButton.setFocus();
					}
					else {
						isDialogOpen = true;
						projectLocationSelected();
					}
			}
		});
		projectLocation.addListener(SWT.MouseEnter, new Listener () {
			public void handleEvent(Event e) {
			    applyStatus(new Status(IStatus.INFO, "projectLocation", Messages.ProjectLocation_desc1)); //$NON-NLS-1$
			}
		});
		projectLocation.addMouseTrackListener(mouseExit);
		projectLocationButton = new Button(composite, SWT.PUSH);
		projectLocationButton.setText(Messages.Browse);
		projectLocationButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				projectLocationSelected();
			}
		});
	}

	/**
	 * This function is called when project location was selected.
	 * It validates the selected project and applies the status associated
	 */
	protected void projectLocationSelected() {
			String fn = SWTUtils.openFolderDialog(getShell());
			if (fn != null){
				titanUtils = new TitanUtils(fn);
				try {
					IStatus status = titanUtils.validateProject();
					if(status.getSeverity() != IStatus.ERROR)
						if(titanUtils.existEclipseProject())
							status = new Status(IStatus.WARNING, "projectLocation", Messages.EclipseProjTaken); //$NON-NLS-1$
					projectLocation.setText(fn);
					applyStatus(status);
					updateButtons();
				} catch (Exception e) {
					MessageDialog.openError(getShell(), Messages.Error, e.getMessage());
				}
			}
	}

	/**
	 * Import the informed project to Eclipse
	 * @return true if all went OK and false otherwise
	 */
	public boolean importProject() {
		try {
			titanUtils.importProject();
			return true;
		} catch (Exception e) {
			MessageDialog.openError(getShell(), Messages.Error, e.getMessage());
			return false;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	protected void applyStatus(IStatus new_status) {
		if(new_status.getSeverity() != IStatus.INFO)
			projectLocationStatus = new_status;
		applyPageStatus(new_status);
	}

	/**
	 * {@inheritDoc}
	 */
	protected IStatus greaterStatus() {
		return projectLocationStatus;
	}

	/**
	 * Returns whether this page is completed
	 * @return true if the wizard could be finished, and false otherwise
	 */
	public boolean canFinish() {
		if (pageStatus.getSeverity() == IStatus.ERROR || projectLocation.getText().isEmpty()) //$NON-NLS-1$
			return false;
		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean canFlipToNextPage() {
		return false;
	}
}
